﻿using UnityEngine;
using System.Collections;

public class RobotManager : MonoBehaviour
{
	public Animator[] robots;
	bool initKey = false;
	Ray ray;
	RaycastHit hit;
	public AudioSource shoot;
	public AudioSource gotHitAudio;
	Ray ray02;
	RaycastHit hit02;
	public Transform particleObj;
	public LayerMask robotLayer;
	// Use this for initialization
	void Start ()
	{
//		Application.targetFrameRate = 60;
		Screen.orientation = ScreenOrientation.LandscapeLeft;
	}
	
	void FoundTarget (string state)
	{
		if (state == "1") {
			if (initKey == false) {
				initKey = true;
				InitRobot ();
				Invoke ("StopAll", 5);
			}
//			particleObj.transform.position = targetPos.position;
//			particleObj.rotation = targetPos.rotation;
			particleObj.GetComponent<ParticleSystem> ().enableEmission = true;
		} else {
//			particleObj.GetComponent<ParticleSystem> ().enableEmission = false;
		}
	}
	
	void InitRobot ()
	{
		R1Moveforword ();
		R2Moveforword ();
		R3Moveforword ();
		R4Moveforword ();
	}
 	
	void StopAll ()
	{
		R1Idle ();
		R2Idle ();
		R3Idle ();
		R4Idle ();
	}
 	
	void R1Moveforword ()
	{
		robots [0].Play ("run");
		InvokeRepeating ("R1Walking", 0, 0.02f);
	}
	
	void R1Walking ()
	{
		robots [0].transform.Translate (new Vector3 (0, 0, 3f), Space.Self);
	}
	
	void R1Idle ()
	{
		robots [0].Play ("idle_1");
		CancelInvoke ("R1Walking"); 
	}
	
	void R2Moveforword ()
	{
		robots [1].Play ("run");
		
		InvokeRepeating ("R2Walking", 0, 0.02f);
	}
	
	void R2Walking ()
	{
		robots [1].transform.Translate (new Vector3 (0, 0, 3f), Space.Self);
	}
	
	void R2Idle ()
	{
		robots [1].Play ("idle_2");
		CancelInvoke ("R2Walking");
	}
	
	void R3Moveforword ()
	{
		robots [2].Play ("run");
		
		InvokeRepeating ("R3Walking", 0, 0.02f);
	}
	
	void R3Walking ()
	{
		robots [2].transform.Translate (new Vector3 (0, 0, 3f), Space.Self);
	}
	
	void R3Idle ()
	{
		robots [2].Play ("idle_2");
		CancelInvoke ("R3Walking");
	}
	
	void R4Moveforword ()
	{
		robots [3].Play ("run");
		
		InvokeRepeating ("R4Walking", 0, 0.02f);
	}
	
	void R4Walking ()
	{
		robots [3].transform.Translate (new Vector3 (0, 0, 3), Space.Self);
	}
	
	void R4Idle ()
	{
		robots [3].Play ("idle");
		CancelInvoke ("R4Walking");
	}
	
	void Update ()
	{
		if (Input.GetKeyDown (KeyCode.Mouse0)) {
			ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			if (Physics.Raycast (ray, out hit)) {
				if (hit.collider.name == "Shoot") {
					Debug.Log ("Shoot");
					shoot.Play ();
					ray02 = Camera.main.ScreenPointToRay (Camera.main.ViewportToScreenPoint (new Vector3 (0.5f, 0.5f, 0)));
					if (Physics.Raycast (ray02, out hit02, Mathf.Infinity, robotLayer.value)) {
						if (hit02.collider.tag == "Robot") {
							Debug.Log ("get_damage");
							gotHitAudio.Play ();
							hit02.collider.gameObject.GetComponent<Animator> ().Play ("get_damage");
						}
					}
				}
			}
		}

		if (Input.GetKeyDown (KeyCode.Space)) {
			R1Moveforword ();
			R2Moveforword ();
			R3Moveforword ();
			R4Moveforword ();
		}
		if (Input.GetKeyDown (KeyCode.S)) {
			R1Idle ();
			R2Idle ();
			R3Idle ();
			R4Idle ();
		}
	}
}